package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Autowired
    private IUserService userService;

    @Override
    public Result followOrNot(Long followUserId) {
        UserDTO user = UserHolder.getUser();
        if (user == null){
            return Result.fail("用户未登录");
        }
        Long currentUserId = user.getId();
        List<Follow> list = query().eq("follow_user_id", followUserId)
                .eq("user_id", currentUserId).list();
        if (list != null && list.size() == 1){
            Follow follow = list.get(0);
            return Result.ok(true);
        }else if (list != null && list.size() > 1){
            return Result.fail("数据错误");
        }
        return Result.ok(false);
    }

    @Override
    public Result changeSubscribe(Long followUserId, Boolean isSubscribe) {
        UserDTO user = UserHolder.getUser();
        if (user == null){
            return Result.fail("请先登录");
        }
        if (isSubscribe){
            Follow follow = new Follow();
            follow.setFollowUserId(followUserId);
            follow.setUserId(user.getId());
            this.save(follow);
        }else {
            HashMap<String, Object> cluMap = new HashMap<>();
            cluMap.put("user_id", user.getId());
            cluMap.put("follow_user_id", followUserId);
            removeByMap(cluMap);
        }
        return Result.ok();
    }

    @Override
    public Result commonSubscribe(Long userId) {
        UserDTO user = UserHolder.getUser();
        if (user == null){
            return Result.fail("请先登录");
        }
        List<Follow> targetUserSubs = query().eq("user_id", userId).list();
        List<Follow> currentUserSubs = query().eq("user_id", user.getId()).list();
        HashSet<Long> targetSubIds = new HashSet<>();
        for (Follow follow : targetUserSubs){
            targetSubIds.add(follow.getFollowUserId());
        }
        List<Long> commonUserIds = new ArrayList<>();
        for (Follow follow : currentUserSubs){
            if (targetSubIds.contains(follow.getFollowUserId())) {
                commonUserIds.add(follow.getFollowUserId());
            }
        }
        if (commonUserIds.isEmpty()){
            return Result.ok();
        }
        List<UserDTO> users = userService.listByIds(commonUserIds).stream().map(user1 -> BeanUtil.copyProperties(user1, UserDTO.class)).collect(Collectors.toList());

        return Result.ok(users);
    }
}
